package com.chenzhou.product_sys.controller;

import com.chenzhou.product_sys.entity.Product;
import com.chenzhou.product_sys.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService service;

    @RequestMapping("/get_products")
    public String queryAllProduct(HttpServletRequest request){
        List<Product> lists = service.queryAllProduct();
        request.setAttribute("plist",lists);
        return "product/show_products";
    }

    @RequestMapping("/get_peoductOne")
    public String  queryProductOne(HttpServletRequest request,Integer pid){
        Product product = service.queryProductById(pid);
        request.setAttribute("p",product);
        return "product/modify_product";
    }

    @RequestMapping("/add_product")
    public String add_Product(HttpServletRequest request, Product product){
        int a = service.add_product(product);
        if(a>0){
            return  "redirect:/get_products";//防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/modify_product")
    public String modify_Product(HttpServletRequest request, Product product){
        int a = service.modify_product(product);
        if(a>0){
            return  "redirect:/get_products"; //防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }

    @RequestMapping("/delete_product")
    public String delete_Product(HttpServletRequest request, Integer pid){
        int a = service.delete_product(pid);
        if(a>0){
            return  "forward:/get_products"; //防止重复提交
        }else{
            return "forward:/error.jsp";
        }
    }
}
